package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.Role;
import com.three.mapper.RoleMapper;
import com.three.service.RoleService;
import com.three.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public R getAll() {
        List<Role> all = roleMapper.getAll();
        return R.ok(all);
    }

    @Override
    public R selectByPage(Integer currentPage, Integer pageSize) {
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        List<Role> all = roleMapper.getAll();
        //封装分页数据
        PageInfo<Role> pageInfo = new PageInfo<>(all);
        return R.ok(pageInfo);
    }

    @Override
    public R addRole(Role role) {
        int insert = roleMapper.insert(role);
        return insert>0?R.ok():R.error();
    }

    @Override
    public R getByRoleId(Integer roleid) {
        return R.ok(roleMapper.selectByPrimaryKey(roleid));
    }

    @Override
    public R updateRole(Role role) {
        return R.ok(roleMapper.updateByPrimaryKeySelective(role));
    }

    @Override
    public R deleteRole(Integer roleid) {
        int i = roleMapper.deleteByPrimaryKey(roleid);
        return i>0?R.ok():R.error();
    }

    // 查询所有正常状态下的角色
    @Override
    public R roleAll() {
        List<Role> list=roleMapper.roleAll();
        return R.ok(list);
    }
}
